using System;
using System.ComponentModel;
using System.DirectoryServices;
using System.Management;
using System.Text;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps.BaseClasses
{
    [Serializable]
    public abstract class IISStep : ActionStep
    {
        public string m_webSiteName = string.Empty;
        public string m_propertyPath = StepConstants.NAME;

        [NonSerialized]
        private string m_propertyName = null;

        [NonSerialized]
        private string m_propertyPathOnly = null;

        [DisplayName( "Property Path" )]
        [Description( "The path of the property.  For example \"AnonymousUserName\" or \"Filters/Compression/gzip/HcFileExtensions\"." )]
        public string PropertyPath
        {
            get
            {
                return m_propertyPath;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Property path cannot be empty." );
                }
                else
                {
                    m_propertyPath = value.Trim();
                }
            }
        }

        [DisplayName( "Web Site" )]
        [Description( "The name of the web site.  Leave this empty to use the root Web Sites node." )]
        public string WebSite
        {
            get
            {
                return m_webSiteName;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    m_webSiteName = value;
                }
                else
                {
                    m_webSiteName = value.Trim();
                }
            }
        }

        protected string PropertyName
        {
            get
            {
                if( m_propertyName == null )
                {
                    separatePathFromName();
                }
                return m_propertyName;
            }
        }

        protected string PropertyPathOnly
        {
            get
            {
                if( m_propertyPathOnly == null )
                {
                    separatePathFromName();
                }
                return m_propertyPathOnly;
            }
        }

        protected string displayProperty( object property )
        {
            StringBuilder sb = new StringBuilder();
            if( property == null )
            {
                sb.Append( "NULL" );
            }
            if( property is string || property is bool )
            {
                sb.Append( property.ToString() );
            }
            else
            {
                foreach( object subObject in (Object[]) property )
                {
                    sb.AppendFormat( "{0} ", subObject );
                }
            }

            return sb.ToString();
        }

        protected DirectoryEntry getEntry()
        {
            if( !string.IsNullOrEmpty( m_webSiteName ) )
            {
                // Get site Id
                ManagementObject obj = Common.GetWmiObject( @"\\{0}\root\MicrosoftIISv2",
                    CurrentServer, "SELECT Name FROM IIsWebServerSetting WHERE ServerComment = \"Surname\"" );
                string siteId = obj.Properties[ "Name" ].Value.ToString();
                siteId = siteId.Substring( siteId.LastIndexOf( '/' ) + 1 );

                // Get site entry
                return new DirectoryEntry(
                    string.Format( "IIS://{0}/W3SVC/{1}{2}", CurrentServer, siteId, PropertyPathOnly ) );
            }
            else
            {
                // Get site entry
                return new DirectoryEntry(
                    string.Format( "IIS://{0}/W3SVC{1}", CurrentServer, PropertyPathOnly ) );
            }
        }

        private void separatePathFromName()
        {
            // separate property path from name
            string[] pieces = m_propertyPath.Split( new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries );

            if( pieces.Length < 2 )
            {
                m_propertyName = m_propertyPath;
                m_propertyPathOnly = string.Empty;
            }
            else
            {
                m_propertyName = pieces[ pieces.Length - 1 ];

                // Save all path except for last value.
                StringBuilder sb = new StringBuilder( "/" );
                for( int i = 0 ; i < pieces.Length - 1 ; i++ )
                {
                    sb.AppendFormat( "{0}/", pieces[ i ] );
                }
                sb.Remove( sb.Length - 1, 1 );
                m_propertyPathOnly = sb.ToString();
            }
        }
    }
}
